package com.waterworld.watch.home.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.waterworld.watch.R;
import com.waterworld.watch.common.activity.BaseActivity;
import com.waterworld.watch.common.application.MyApplication;
import com.waterworld.watch.common.bean.BindWatchBean;
import com.waterworld.watch.common.bean.BindWatchUserBean;
import com.waterworld.watch.common.bean.UserInfoBean;
import com.waterworld.watch.common.customview.CustomDialog;
import com.waterworld.watch.common.net.BaseObserverListener;
import com.waterworld.watch.common.net.BaseResultBean;
import com.waterworld.watch.common.util.AppManager;
import com.waterworld.watch.common.util.ScreenAdapterUtil;
import com.waterworld.watch.common.util.ToastUtils;
import com.waterworld.watch.home.interfaces.IHomeManager;
import com.waterworld.watch.home.manager.HomeManager;
import com.waterworld.watch.login.event.AutoLoginEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WatchUnbindActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.header_parent)
    LinearLayout header_parent;

    @BindView(R.id.header_back)
    ImageView header_back;

    @BindView(R.id.header_title)
    TextView header_title;

    @BindView(R.id.rl_unbind)
    RelativeLayout rl_unbind;

    @BindView(R.id.rl_shift_manager)
    RelativeLayout rl_shift_manager;

    @BindView(R.id.rl_unbind_other_user)
    RelativeLayout rl_unbind_other_user;

    @BindView(R.id.rl_factory_setting)
    RelativeLayout rl_factory_setting;

    private CustomDialog mDialog;
    private IHomeManager iHomeManager = HomeManager.getInstance();
    private List<BindWatchUserBean> mList = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_watch_unbind);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        setViewSize(header_parent, ViewGroup.LayoutParams.MATCH_PARENT, ScreenAdapterUtil.getHeightPx(this) / 10);
        header_title.setText(getString(R.string.unbind));
        header_back.setVisibility(View.VISIBLE);
        header_title.setVisibility(View.VISIBLE);

        header_back.setOnClickListener(this);
        rl_unbind.setOnClickListener(this);
        rl_shift_manager.setOnClickListener(this);
        rl_unbind_other_user.setOnClickListener(this);
        rl_factory_setting.setOnClickListener(this);

        if (iHomeManager != null) {
            iHomeManager.listBindWatchUser(new BaseObserverListener<BaseResultBean<BindWatchUserBean[]>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(BaseResultBean<BindWatchUserBean[]> baseResultBean) {
                    mList.addAll(Arrays.asList(baseResultBean.getData()));
                    if (UserInfoBean.getInstance().getWatchUser().getUserRole() == 0) {
                        //是管理员的话可以解绑其他人
                        if (mList.size() == 1) {
                            rl_factory_setting.setVisibility(View.VISIBLE);
                        } else if (mList.size() > 1){
                            rl_shift_manager.setVisibility(View.VISIBLE);
                            rl_unbind_other_user.setVisibility(View.VISIBLE);
                            rl_factory_setting.setVisibility(View.VISIBLE);
                        }
                    } else if (UserInfoBean.getInstance().getWatchUser().getUserRole() == 1) {
                        //不是管理员只能解绑自身
                        rl_unbind.setVisibility(View.VISIBLE);
                    }
                }
            });
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_back:
                finish();
                break;
            case R.id.rl_unbind:
                //解除绑定,仅非管理员可用
                mDialog = new CustomDialog(this, getString(R.string.warm_prompt), getString(R.string.confirm_unbind),new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                        unBindWatch(UserInfoBean.getInstance().getUser().getUserId());
                    }
                }, new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                },getString(R.string.confirm),getString(R.string.cancel));
                mDialog.setCanotBackPress();
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.show();
                break;
            case R.id.rl_shift_manager:
                //移交权限,仅管理员并且还有其他用户绑定时可用
                startActivity(new Intent(WatchUnbindActivity.this,WatchUnbindOtherUserActivity.class).putExtra("title","transferAuthority"));
                break;
            case R.id.rl_unbind_other_user:
                startActivity(new Intent(WatchUnbindActivity.this,WatchUnbindOtherUserActivity.class).putExtra("title","unbindOthers"));
                break;
            case R.id.rl_factory_setting:
                mDialog = new CustomDialog(this, getString(R.string.warm_prompt), getString(R.string.confirm_factory_reset),new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                        resumeFactory();
                    }
                }, new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                },getString(R.string.confirm),getString(R.string.cancel));
                mDialog.setCanotBackPress();
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.show();
                break;
        }
    }

    //解绑手表
    private void unBindWatch(Integer userId) {
        if (iHomeManager != null) {
            iHomeManager.unbindWatch(userId, new BaseObserverListener<BaseResultBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(BaseResultBean baseResultBean) {
                    if (baseResultBean.getCode() == 0) {
                        switchWatch();
                    }
                }
            });
        }
    }

    //恢复出厂设置
    private void resumeFactory() {
        if (iHomeManager != null) {
            iHomeManager.resumeFactory(new BaseObserverListener<BaseResultBean>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onNext(BaseResultBean baseResultBean) {
                    if (baseResultBean.getCode() == 0) {
                        switchWatch();
                    }
                }
            });
        }
    }

    //解绑/恢复出厂设置都需要主动切换手表
    private void switchWatch() {
        //若该用户还有绑定手表，跳转到主页，刷新内容
        //若无绑定手表，跳转到绑定手表页面
        iHomeManager.listBindWatch(new BaseObserverListener<BaseResultBean<BindWatchBean[]>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(BaseResultBean<BindWatchBean[]> baseResultBean) {
                if (baseResultBean.getData().length != 0) {
                    //还有其他绑定手表
                    iHomeManager.switchWatch(baseResultBean.getData()[0].getWatchId(), new BaseObserverListener<BaseResultBean>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            Log.e("nh1",e.toString());
                        }

                        @Override
                        public void onNext(BaseResultBean baseResultBean) {
                            if (baseResultBean.getCode() == 0) {
                                AppManager.finishActivity(WatchUnbindActivity.class);
                                AppManager.finishActivity(WatchInfoActivity.class);
                                EventBus.getDefault().post(new AutoLoginEvent(1,null));
                            }
                        }
                    });
                } else {
                    //无手表需要切换手表到null
                    iHomeManager.switchWatch(null, new BaseObserverListener<BaseResultBean>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                        }

                        @Override
                        public void onNext(BaseResultBean baseResultBean) {
                            if (baseResultBean.getCode() == 0) {
                                AppManager.finishAllActivity();
                                startActivity(new Intent(MyApplication.getContext(), WatchBindActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                            }
                        }
                    });
                }
            }
        });
    }

}
